/**
 * Created by wenchuan on 2018/03/06.
 */
define(function () {

    var self,wxxx_page;

    function Wdbg() {
        self = this;
        self.mData = {
            orderSetItem: {
                page: 0,
                limit: 20,
                start: function () {
                    return this.page * this.limit;
                },
                data: [],
                details: [],
                mmra:{
                    MMRA004: '',
                    MMRA003: '',
                    GWAKL004: '',
                    MMRA010: '',
                    MMRA012: '',
                    MMRA013: ''
                }
            }
        };
        wxxx_page = app.pageParameters['wxxx_page'];
        //页面传过来的数据
        self.mData.orderSetItem.details = app.cloneJSON(wxxx_page.obj);
        self.getMmraData();
        self.init();
    }

    //数据获取
    Wdbg.prototype.getData = function (refresh) {
        var order = self.mData.orderSetItem;

        app.ajax({
            xurl: "api/wx/fwbg/getMmrdList",
            xdata: {
                'page':order.page+1,
                'start': order.start(),
                'limit': order.limit,
                'MMRD002':order.details.MMRF003,
                'MMRD003':order.details.MMRF004
            },
            cb: function (result) {
                if (result.ok) {
                    var data = result.data;
                    order.data.pushArray(data.data, order.page == 0);

                    if (order.page == 0) {
                        refresh.endPulldownToRefresh();
                        if (order.data.length ==0) {
                            app.showNoData(order._tip, '你还没有相关维修记录');
                            refresh.disablePullupToRefresh();
                        } else {
                            app.hideNoData(order._tip);
                            refresh.refresh(true);
                        }
                    }
                    order.page++;
                    refresh.endPullupToRefresh(data.data.length < order.limit);
                }
            }
        });
    };
    // 获取服务费列表
    Wdbg.prototype.getMmraData = function () {
        app.ajax({
            xurl: "api/wx/fwbg/getMmraData",
            xtime: '1d',
            xdata: {
                MMRD002:self.mData.orderSetItem.details.MMRF003,
                MMRD003:self.mData.orderSetItem.details.MMRF004
            },
            cb: function (result) {
                if (result.ok) {
                    self.mData.orderSetItem.mmra.MMRA004 = result.data.data.MMRA004;
                    self.mData.orderSetItem.mmra.MMRA003 = result.data.data.MMRA003;
                    self.mData.orderSetItem.mmra.GWAKL004 = result.data.data.GWAKL004;
                    self.mData.orderSetItem.mmra.MMRA010 = result.data.data.MMRA010;
                    self.mData.orderSetItem.mmra.MMRA012 = result.data.data.MMRA012;
                    if(result.data.data.MMRA013 == '1'){
                        self.mData.orderSetItem.mmra.MMRA013 = '是';
                    }else if(result.data.data.MMRA013 == '0'){
                        self.mData.orderSetItem.mmra.MMRA013 = '否';
                    }else if(result.data.data.MMRA013 == '2'){
                        self.mData.orderSetItem.mmra.MMRA013 = '延保';
                    }
                }
            }
        });
    };

    Wdbg.prototype.init = function () {

        mui.ready(function () {

            Vue.component('wxxx-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array
                },
                methods: {
                    showDetail: function (item) {
                        app.openPage({
                            src: "fwbg/scene.html",
                            argsKey: 'scene_page',
                            args: {
                                obj: item,
                                cb: function () {
                                    //刷新数据
                                    self.mData.orderSetItem.refresh.pulldownLoading();
                                }
                            }
                        });
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'pullRefresh0'),
                data: {
                    items: self.mData.orderSetItem.data
                }
            });
           new Vue({
                el: app.getByid(doc, 'mmra'),
                data: {
                    des: self.mData.orderSetItem.mmra
                }
            });
            self.mData.orderSetItem._tip = $('#pullRefresh0');
            mui('#pullRefresh0').pullRefresh({
                down: {
                    callback: function () {
                        self.mData.orderSetItem.refresh = this;
                        self.mData.orderSetItem.page = 0;
                        self.getData(this);
                    },
                    auto: true
                },
                up: {
                    callback: function () {
                        self.getData(this);
                    }
                }
            });
            app.taps.call(win, [
                {
                    o: 'cancle',
                    cb: function (e) {
                        app.back(1);
                    }
                }
            ]);
        });
    };

    return new Wdbg();
});